home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / UI / UI_BOXES.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  10KB  |  361 lines

  1. /****************************************************************
  2.  
  3.     ui_boxes.c      Box drawing and window routines for
  4.             The Bywater Graphical User Interface
  5.  
  6.             Copyright (c) 1991, Ted A. Campbell
  7.  
  8.             Bywater Software
  9.             P. O. Box 4023
  10.             Duke Station
  11.             Durham, NC  27706
  12.  
  13.             email: tcamp@hercules.acpub.duke.edu
  14.  
  15.     Copyright and Permissions Information:
  16.  
  17.     All U.S. and international copyrights are claimed by the
  18.     author. The author grants permission to use this code
  19.     and software based on it under the following conditions:
  20.     (a) in general, the code and software based upon it may be
  21.     used by individuals and by non-profit organizations; (b) it
  22.     may also be utilized by governmental agencies in any country,
  23.     with the exception of military agencies; (c) the code and/or
  24.     software based upon it may not be sold for a profit without
  25.     an explicit and specific permission from the author, except
  26.     that a minimal fee may be charged for media on which it is
  27.     copied, and for copying and handling; (d) the code must be
  28.     distributed in the form in which it has been released by the
  29.     author; and (e) the code and software based upon it may not
  30.     be used for illegal activities.
  31.  
  32. ****************************************************************/
  33.  
  34. #include "stdio.h"
  35. #include "bw.h"
  36. #include "gr.h"
  37. #include "ui.h"
  38.  
  39. #ifdef     __STDC__
  40. #include "malloc.h"
  41. #else
  42. extern  char * malloc();
  43. #endif
  44.  
  45. struct uiwindow *
  46. ui_window( x1, y1, x2, y2, t_flag, t_bcolor, t_tcolor, t_text,
  47.    b_flag, b_color,
  48.    s_flag, s_color, m_color, m_style, buttons )
  49.    int x1, y1, x2, y2;          /* coordinates of whole area    */
  50.    int t_flag;                  /* title flag         */
  51.    int t_bcolor, t_tcolor;      /* title background & text colors */
  52.    char *t_text;                /* text of title      */
  53.    int b_flag, b_color;         /* border flag, color      */
  54.    int s_flag, s_color;         /* shadow flag, color      */
  55.    int m_color, m_style;        /* color & style for main box   */
  56.    int buttons;                 /* buttons */
  57.    {
  58.    static int _x1, _y1, _x2, _y2;  /* counters for coordinates     */
  59.    static struct uiwindow *ret_struct;     /* structure for return data    */
  60.    int s_ysize;
  61.  
  62.    if ( ui_ready == 0 )
  63.       {
  64. #ifdef DEBUG
  65.       bw_error( "The ui system is not initialized." );
  66. #endif
  67.       return;
  68.       }
  69.  
  70.    /* Get memory for window structure */
  71.  
  72.    if ( ( ret_struct = (struct uiwindow *) malloc( sizeof( struct uiwindow ) )) == 0 )
  73.       {
  74.       bw_error( "Cannot allocate storage for window data." );
  75.       return;
  76.       }
  77.  
  78.    /* Initialize counters */
  79.  
  80.    ret_struct->x1 = _x1 = x1;
  81.    ret_struct->y1 = _y1 = y1;
  82.    ret_struct->x2 = _x2 = x2;
  83.    ret_struct->y2 = _y2 = y2;
  84.    ret_struct->t_flag = t_flag;
  85.    ret_struct->t_bcolor = t_bcolor;
  86.    ret_struct->t_tcolor = t_tcolor;
  87.    ret_struct->s_flag = s_flag;
  88.    ret_struct->s_color = s_color;
  89.    ret_struct->b_flag = b_flag;
  90.    ret_struct->b_color = b_color;
  91.    ret_struct->m_color = m_color;
  92.    ret_struct->m_style = m_style;
  93.  
  94.    /* display the shadow */
  95.  
  96.    if ( s_flag == TRUE )
  97.       {
  98.       s_ysize = (SHADOW_XSIZE * gr_pxsize ) / gr_pysize;
  99.       ui_fbox( x1 + SHADOW_XSIZE, y1, x2, y2 - s_ysize,
  100.      s_color, SOLID );
  101.       _y1 += s_ysize;
  102.       _x2 -= SHADOW_XSIZE;
  103.       }
  104.  
  105.    /* draw a one-pixel black line around the whole area */
  106.  
  107.    gr_line( ui_screen, _x1, _y1, _x1, _y2, BLACK, SOLID );
  108.    gr_line( ui_screen, _x1, _y2, _x2, _y2, BLACK, SOLID );
  109.    gr_line( ui_screen, _x2, _y1, _x2, _y2, BLACK, SOLID );
  110.    gr_line( ui_screen, _x1, _y1, _x2, _y1, BLACK, SOLID );
  111.  
  112.    ++_x1;
  113.    ++_y1;
  114.    --_x2;
  115.    --_y2;
  116.  
  117.    /* display the title box */
  118.  
  119.    if ( t_flag != 0 )
  120.       {
  121.  
  122.       /* calculate size of the title bar */
  123.  
  124.       ret_struct->tbar_x1 = _x1;
  125.       ret_struct->tbar_y1 = _y2 - ( ui_grwind->fysize
  126.      + TITLE_YMARGIN + TITLE_YMARGIN );
  127.       ret_struct->tbar_x2 = _x2;
  128.       ret_struct->tbar_y2 = _y2;
  129.       _y2 = ret_struct->tbar_y1 - 1;
  130.  
  131.       /* blank the whole title bar area before buttons */
  132.  
  133.       gr_rectangle( ui_screen, ret_struct->tbar_x1, ret_struct->tbar_y1,
  134.      ret_struct->tbar_x2, ret_struct->tbar_y2, ret_struct->t_bcolor,
  135.      SOLID );
  136.  
  137.       ret_struct->ti_x1 = ret_struct->tbar_x1;
  138.       ret_struct->ti_y1 = ret_struct->tbar_y1;
  139.       ret_struct->ti_x2 = ret_struct->tbar_x2;
  140.       ret_struct->ti_y2 = ret_struct->tbar_y2;
  141.  
  142.       ret_struct->buttons = buttons;
  143.  
  144.       /* calculate close button area and display the button */
  145.  
  146.       if ( ( buttons & BUT_CLOSE ) > 0 )
  147.      {
  148.      ret_struct->bt_x1 = ret_struct->ti_x1 + ui_grwind->fxsize;
  149.      ret_struct->bt_y1 = ret_struct->ti_y1 + TITLE_YMARGIN;
  150.      ret_struct->bt_x2 = ret_struct->ti_x1
  151.         + ( ui_grwind->fxsize * 3 );
  152.      ret_struct->bt_y2 = ret_struct->ti_y2 - TITLE_YMARGIN;
  153.      ret_struct->ti_x1 = ret_struct->bt_x2 + 1;
  154.      ui_pbmcenter( ui_screen, ret_struct->bt_x1, ret_struct->bt_y1,
  155.         ret_struct->bt_x2, ret_struct->bt_y2, &ui_clicon );
  156.      }
  157.  
  158.       /* calculate move button area and display the button */
  159.  
  160.       if ( ( buttons & BUT_MOVE ) > 0 )
  161.      {
  162.      ret_struct->mv_x1 = ret_struct->ti_x2 - ( ui_grwind->fxsize * 3 );
  163.      ret_struct->mv_y1 = ret_struct->ti_y1 + TITLE_YMARGIN;
  164.      ret_struct->mv_x2 = ret_struct->ti_x2 - 2;
  165.      ret_struct->mv_y2 = ret_struct->ti_y2 - TITLE_YMARGIN;
  166.      ret_struct->ti_x2 = ret_struct->mv_x1 - 1;
  167.      ui_pbmcenter( ui_screen, ret_struct->mv_x1, ret_struct->mv_y1,
  168.         ret_struct->mv_x2, ret_struct->mv_y2, &ui_mvicon );
  169.      }
  170.  
  171.       /* calculate resize button area and display the button */
  172.  
  173.       if ( ( buttons & BUT_RESIZE ) > 0 )
  174.      {
  175.      ret_struct->re_x1 = ret_struct->ti_x2 - ( ui_grwind->fxsize * 3 );
  176.      ret_struct->re_y1 = ret_struct->ti_y1 + TITLE_YMARGIN;
  177.      ret_struct->re_x2 = ret_struct->ti_x2 - 2;
  178.      ret_struct->re_y2 = ret_struct->ti_y2 - TITLE_YMARGIN;
  179.      ret_struct->ti_x2 = ret_struct->re_x1 - 1;
  180.      ui_pbmcenter( ui_screen, ret_struct->re_x1, ret_struct->re_y1,
  181.         ret_struct->re_x2, ret_struct->re_y2, &ui_reicon );
  182.      }
  183.  
  184.       /* Draw a line under the title bar */
  185.  
  186.       gr_line( ui_screen, x1, _y2, _x2, _y2, s_color, SOLID );
  187.      _y2 -= 1;
  188.  
  189.       /* Display the text */
  190.  
  191.       ui_wtitle( ret_struct, t_text );
  192.  
  193.       }
  194.  
  195.    /* display the main area */
  196.  
  197.    ui_fbox( _x1, _y1, _x2, _y2, m_color, m_style );
  198.  
  199.    /* display the border */
  200.  
  201.    if ( b_flag == TRUE )
  202.       {
  203.       _x1 += BORDER_SIZE;
  204.       _y1 += BORDER_SIZE;
  205.       _x2 -= BORDER_SIZE;
  206.       _y2 -= BORDER_SIZE;
  207.       ui_hbox( _x1, _y1, _x2, _y2, b_color );
  208.       _x1 += 1;
  209.       _y1 += 1;
  210.       _x2 -= 1;
  211.       _y2 -= 1;
  212.       }
  213.  
  214.    /* return coordinates of usable area */
  215.  
  216.    ret_struct->u_x1 = _x1;
  217.    ret_struct->u_y1 = _y1;
  218.    ret_struct->u_x2 = _x2;
  219.    ret_struct->u_y2 = _y2;
  220.    return ret_struct;
  221.  
  222.    }
  223.  
  224. ui_rewindow( window, newtitle )
  225.    struct uiwindow *window;
  226.    char *newtitle;
  227.    {
  228.    static int _x1, _y1, _x2, _y2;  /* counters for coordinates     */
  229.    int s_ysize;
  230.  
  231.    if ( window == 0 )
  232.       {
  233.       return;
  234.       }
  235.  
  236.    _x1 = window->x1;
  237.    _y1 = window->y1;
  238.    _x2 = window->x2;
  239.    _y2 = window->y2;
  240.  
  241.    /* display the shadow */
  242.  
  243.    if ( window->s_flag != 0 )
  244.       {
  245.       s_ysize = (SHADOW_XSIZE * gr_pxsize ) / gr_pysize;
  246.       ui_fbox( window->x1 + SHADOW_XSIZE, window->y1,
  247.      window->x2, window->y2 - s_ysize,
  248.      window->s_color, 1 );
  249.       _y1 += s_ysize;
  250.       _x2 -= SHADOW_XSIZE;
  251.       }
  252.  
  253.    /* draw a one-pixel black line around the whole area */
  254.  
  255.    gr_line( ui_screen, _x1, _y1, _x1, _y2, BLACK, SOLID );
  256.    gr_line( ui_screen, _x1, _y2, _x2, _y2, BLACK, SOLID );
  257.    gr_line( ui_screen, _x2, _y1, _x2, _y2, BLACK, SOLID );
  258.    gr_line( ui_screen, _x1, _y1, _x2, _y1, BLACK, SOLID );
  259.  
  260.    ++_x1;
  261.    ++_y1;
  262.    --_x2;
  263.    --_y2;
  264.  
  265.    /* display the title box */
  266.  
  267.    if ( window->t_flag == TRUE )
  268.       {
  269.  
  270.       gr_rectangle( ui_screen, window->tbar_x1, window->tbar_y1,
  271.      window->tbar_x2, window->tbar_y2, window->t_bcolor,
  272.      SOLID );
  273.  
  274.       ui_wtitle( window, newtitle );
  275.  
  276.       /* Draw a line under the title bar */
  277.  
  278.       _y2 = window->tbar_y1;
  279.       _y2 -= 1;
  280.       gr_line( ui_screen, window->x1, _y2, _x2, _y2,
  281.      window->s_color, SOLID );
  282.       _y2 -= 1;
  283.  
  284.       /* display close button */
  285.  
  286.       if ( ( window->buttons & BUT_CLOSE ) > 0 )
  287.      {
  288.      ui_pbmcenter( ui_screen, window->bt_x1, window->bt_y1,
  289.         window->bt_x2, window->bt_y2, &ui_clicon );
  290.      }
  291.  
  292.       /* display move button */
  293.  
  294.       if ( ( window->buttons & BUT_MOVE ) > 0 )
  295.      {
  296.      ui_pbmcenter( ui_screen, window->mv_x1, window->mv_y1,
  297.         window->mv_x2, window->mv_y2, &ui_mvicon );
  298.      }
  299.  
  300.       /* display resize button */
  301.  
  302.       if ( ( window->buttons & BUT_RESIZE ) > 0 )
  303.      {
  304.      ui_pbmcenter( ui_screen, window->re_x1, window->re_y1,
  305.         window->re_x2, window->re_y2, &ui_reicon );
  306.      }
  307.  
  308.       }
  309.  
  310.    /* display the main area */
  311.  
  312.    ui_fbox( _x1, _y1, _x2, _y2, window->m_color, window->m_style );
  313.  
  314.    /* display the border */
  315.  
  316.    if ( window->b_flag != 0 )
  317.       {
  318.       _x1 += BORDER_SIZE;
  319.       _y1 += BORDER_SIZE;
  320.       _x2 -= BORDER_SIZE;
  321.       _y2 -= BORDER_SIZE;
  322.       ui_hbox( _x1, _y1, _x2, _y2, window->b_color );
  323.       }
  324.  
  325.    }
  326.  
  327. ui_fbox( x1, y1, x2, y2, color, style )
  328.    int x1, y1, x2, y2, color, style;
  329.    {
  330. #ifdef  OLD_DEBUG
  331.    sprintf( bw_ebuf, "[pr:] ui_fbox(): %d %d %d %d", x1, y1, x2, y2 );
  332.    bw_error( bw_ebuf );
  333. #endif
  334.    gr_rectangle( ui_screen, x1, y1, x2, y2, color, style );
  335.    }
  336.  
  337. ui_hbox( x1, y1, x2, y2, color )
  338.    int x1, y1, x2, y2, color;
  339.    {
  340.    gr_rectangle( ui_screen, x1, y1, x2, y2, color, HOLLOW );
  341.    }
  342.  
  343. ui_wtitle( window, buffer )
  344.    struct uiwindow *window;
  345.    char *buffer;
  346.    {
  347.  
  348.    /* show the title bar */
  349.  
  350.    gr_rectangle( ui_screen, window->ti_x1, window->ti_y1,
  351.       window->ti_x2, window->ti_y2, window->t_bcolor,
  352.       SOLID );
  353.  
  354.    /* display the title text */
  355.  
  356.    ui_str( window->ti_x1 + ui_grwind->fxsize,
  357.       window->ti_y1 + TITLE_YMARGIN, window->ti_x2,
  358.       window->t_bcolor, window->t_tcolor, buffer );
  359.  
  360.    }
  361.